package com.cea.papyrus.core.listener;

import java.util.Iterator;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.NotificationImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.utils.Debug;

public class PapyrusRemoveListener extends EContentAdapter {

	public void notifyChanged(Notification notification)
	{

		super.notifyChanged(notification);

		if (notification.getEventType()==Notification.REMOVE){
			Debug.debug(this, ""+notification, 64000);
			if(notification.getOldValue() instanceof Element) {
				//We have observed that when we disconnect a Dependency
				// it send Remove notification instead of SET -1
				// So we have been forced to test in this is not a dependency
				if(!(notification.getNotifier() instanceof Dependency)){
					Element deletedElement= (Element)notification.getOldValue();
					deletedElement.eNotify(new NotificationImpl(PapyrusNotification.ELEMENT_DELETED, deletedElement, null));
					Iterator<EObject> ite= deletedElement.eContents().iterator();
					while(ite.hasNext()){

						EObject signalElt= (EObject)ite.next();
						if( signalElt instanceof Element){
							signalElt.eNotify(new NotificationImpl(PapyrusNotification.ELEMENT_DELETED, signalElt, null));
						}
					}
				}
			}

			else if (notification.getOldValue() instanceof EObject){
				EObject deletedEObject= (EObject)notification.getOldValue();
				deletedEObject.eNotify(new NotificationImpl(PapyrusNotification.ELEMENT_DELETED, deletedEObject, null));
				Iterator<EObject> ite= deletedEObject.eContents().iterator();
				while(ite.hasNext()){

					EObject signalElt= (EObject)ite.next();
					if( signalElt instanceof Element){
						signalElt.eNotify(new NotificationImpl(PapyrusNotification.ELEMENT_DELETED, signalElt, null));
					}
				}
			}

		}
	}

}
